"""
    存储有一些简单的动画效果
"""
import pygame.image

from PyPixelCore.Setting import *
from PyPixelCore.animation_factory.WorkPiece import WorkPiece
from PyPixelCore.block.TextBlock import TextBlock
from PyPixelCore.panel.Panel import Panel
from PyPixelCore.timer.OnlyTimer import OnlyTimer

# 尝试工厂模式
class TextAnimationFactory:
    # 提醒动画, 在框的中间浮动后向上渐隐消失
    @staticmethod
    def flex_text(text_content, aim_panel: Panel, bk_color=None,disappear_speed=6, font_color=Color_type.RED, offset_y = 0.2):
        def flex_animation_help_2(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐消失
            self.rect.y -= 1
            self.alpha_index -= disappear_speed
            if self.alpha_index > 0:
                self.image.set_alpha(self.alpha_index)
                self.is_dirty = True
            else:
                self.kill()
        def flex_animation_help_1(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐向上
            self.rect.y -= 1
            self.is_dirty = True
        def flex_animation_temp_stay_help(self: TextBlock):  # 一个显示块的帮助函数,帮助临时维持较高的alpha值
            self.delete_frame_animation()
            self.set_frame_animation(flex_animation_help_2)
        temp_message_block = TextBlock(text_content, border_width=0, is_alpha=True)
        temp_message_block.image.convert_alpha()
        temp_message_block.set_bk_color(bk_color)
        temp_message_block.rect.center = (aim_panel.rect.center[0] - aim_panel.point_x,
                                          aim_panel.rect.center[1] - aim_panel.point_y - aim_panel.height * offset_y)
        temp_message_block.set_font_color(font_color)
        # aim_panel.add_sprites(temp_message_block)
        temp_message_block.alpha_index = 255
        temp_message_block.set_timer_animation(OnlyTimer(40), flex_animation_temp_stay_help)
        temp_message_block.set_frame_animation(flex_animation_help_1)

        def run_thing(self: WorkPiece, panel):
            temp_message_block.kill()
            temp_message_block.delete_timer_animation(flex_animation_temp_stay_help)
            temp_message_block.delete_frame_animation()
            temp_message_block.rect.center = (aim_panel.rect.center[0] - aim_panel.point_x,
                                          aim_panel.rect.center[1] - aim_panel.point_y - aim_panel.height * offset_y)
            panel.add_sprites(temp_message_block)
            temp_message_block.alpha_index = 255
            temp_message_block.set_timer_animation(OnlyTimer(40), flex_animation_temp_stay_help)
            temp_message_block.set_frame_animation(flex_animation_help_1)

        return WorkPiece(temp_message_block, run_func=run_thing)

    # 提醒动画, 在原地一段时间,然后逐渐消失
    @staticmethod
    def disappear_text(text_content, aim_panel: Panel, bk_color=None, font_color=Color_type.RED, offset_y=0.3,
                               show_time=1, disappear_speed=5):
        def flex_animation_help(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐消失
            self.alpha_index -= disappear_speed
            if self.alpha_index > 0:
                self.image.set_alpha(self.alpha_index)
                self.is_dirty = True
            else:
                self.kill()

        def flex_animation_temp_stay_help(self: TextBlock):  # 一个显示块的帮助函数,帮助临时维持较高的alpha值
            self.set_frame_animation(flex_animation_help)

        temp_message_block = TextBlock(text_content, border_width=0, is_alpha=True)
        temp_message_block.image.convert_alpha()
        temp_message_block.set_bk_color(bk_color)
        temp_message_block.rect.center = (aim_panel.rect.center[0] - aim_panel.point_x,
                                          aim_panel.rect.center[1] - aim_panel.point_y - aim_panel.height * offset_y)
        temp_message_block.set_font_color(font_color)
        temp_message_block.alpha_index = 255
        temp_message_block.set_timer_animation(OnlyTimer(show_time * fps_limit), flex_animation_temp_stay_help)
        return WorkPiece(temp_message_block)


# 提醒动画, 在框的中间浮动后向上渐隐消失
def flex_animation(text_content, aim_panel: Panel, font_size=basic_font_size, bk_color=None, font_color=Color_type.RED, offset_y = 0.2):
    def flex_animation_help_2(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐消失
        self.rect.y -= 1
        self.alpha_index -= 5
        if self.alpha_index > 0:
            self.image.set_alpha(self.alpha_index)
            self.is_dirty = True
        else:
            self.kill()
    def flex_animation_help_1(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐向上
        self.rect.y -= 1
        self.is_dirty = True
    def flex_animation_temp_stay_help(self: TextBlock):  # 一个显示块的帮助函数,帮助临时维持较高的alpha值
        self.delete_frame_animation()
        self.set_frame_animation(flex_animation_help_2)
    temp_message_block = TextBlock(text_content, font_size=font_size, border_width=0, is_alpha=True)
    temp_message_block.image.convert_alpha()
    temp_message_block.set_bk_color(bk_color)
    temp_message_block.rect.center = (aim_panel.rect.center[0] - aim_panel.point_x, aim_panel.rect.center[1] - aim_panel.point_y - aim_panel.height*offset_y)
    temp_message_block.set_font_color(font_color)
    aim_panel.add_sprites(temp_message_block)
    temp_message_block.alpha_index = 255
    temp_message_block.set_timer_animation(OnlyTimer(40), flex_animation_temp_stay_help)
    temp_message_block.set_frame_animation(flex_animation_help_1)

# 提醒动画, 在原地一段时间,然后逐渐消失
def disappearing_animation(text_content, aim_panel: Panel, bk_color=None, font_size=basic_font_size, font_color=Color_type.RED, offset_y = 0.3, show_time=1, disappear_speed = 5):
    def flex_animation_help(self: TextBlock):  # 一个显示块的帮助函数，帮助显示块逐渐消失
        self.alpha_index -= disappear_speed
        if self.alpha_index > 0:
            self.image.set_alpha(self.alpha_index)
            self.is_dirty = True
        else:
            self.kill()
    def flex_animation_temp_stay_help(self: TextBlock):  # 一个显示块的帮助函数,帮助临时维持较高的alpha值
        self.set_frame_animation(flex_animation_help)
    temp_message_block = TextBlock(text_content, font_size=font_size, border_width=0, is_alpha=True)
    temp_message_block.image.convert_alpha()
    temp_message_block.set_bk_color(bk_color)
    temp_message_block.rect.center = (aim_panel.rect.center[0] - aim_panel.point_x,
                                      aim_panel.rect.center[1] - aim_panel.point_y - aim_panel.height * offset_y)
    temp_message_block.set_font_color(font_color)
    aim_panel.add_sprites(temp_message_block)
    temp_message_block.alpha_index = 255
    temp_message_block.set_timer_animation(OnlyTimer(show_time*fps_limit), flex_animation_temp_stay_help)



